package org.sport.presenter;

import net.sf.json.JSONObject;
import org.sport.logic.Logic_Sport;
import org.sport.model.jsonModel.Json_2_state_ride;
import org.sport.model.jsonModel.Json_2_state_run;
import org.sport.model.jsonModel.Json_2_state_walk;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zehong
 *
 * 主要用于对接增加运动数据功能，完成不行，跑步，骑行的数据记录需求的接口。
 *
 */

@Controller
public class Interface_Sport {


    @Autowired
    Logic_Sport logicForsport;


    /**
     * 功能：记录步行记录
     * json参数：包含步行的一系列情况
     * 返回：添加了增加记录的结果
     */
    @RequestMapping(value = "/2/state_walk", method = RequestMethod.POST)
    public void addWalk(@RequestBody Json_2_state_walk json_2_state_walk
            , HttpServletRequest request
            , HttpServletResponse response
    )
    {
        System.out.println("walk");
        json_2_state_walk = logicForsport.saveWalk(json_2_state_walk);
        try {
            response.getWriter().print(JSONObject.fromObject(json_2_state_walk).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 功能：记录跑步记录
     * json参数：包含跑步的一系列情况
     * 返回：添加了增加记录的结果
     */
    @RequestMapping(value = "/2/state_run", method = RequestMethod.POST)
    public void addRun(@RequestBody Json_2_state_run json_2_state_run
            , HttpServletRequest request
            , HttpServletResponse response
    )
    {
        System.out.println("run");
        json_2_state_run = logicForsport.saveRun(json_2_state_run);
        try {
            response.getWriter().print(JSONObject.fromObject(json_2_state_run).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 功能：记录骑行记录
     * json参数：包含骑行的一系列情况
     * 返回：添加了增加记录的结果
     */
    @RequestMapping(value = "/2/state_ride", method = RequestMethod.POST)
    public void addRide(@RequestBody Json_2_state_ride json_2_state_ride
            , HttpServletRequest request
            , HttpServletResponse response
    )
    {
        System.out.println("ride");
        json_2_state_ride = logicForsport.saveRide(json_2_state_ride);
        try {
            response.getWriter().print(JSONObject.fromObject(json_2_state_ride).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
